<?

/* * ************************************************************************
 * * name: authorize_process()
 * * copyright: mwattier, rewrite sparky - epiphanydev.com
 * * description:  Sends and receives information to authorize.net
 * * Authorize doc: login ==>Help, Doc & Ref Guides ==> AIM implementation w/pswd
 * * parameters:
 * *  '', "; // first field, payID is blank for add
 * *  '".$dt."', ";//transDate == Date
 * *  '1', ";//edProcess: 1 = initial create
 * *  '".$_REQUEST["category"]."', ";
 * *  '0', ";//authCode, provided by authorize later
 * *  '".$_REQUEST["email"]."', ";
 * *  '".$_REQUEST["website"]."', ";
 * *  '".$_REQUEST["transType"]."', ";
 * *  '".$_REQUEST["invoiceNumber"]."', ";
 * *  '".$_REQUEST["description"]."', ";
 * *  '".$_REQUEST["cardName"]."', ";
 * *  '".$_REQUEST["cardType"]."', ";
 * *  '".$_REQUEST["cardNumber"]."', ";
 * *  '".$_REQUEST["cardExpiration"]."', ";
 * *  '".$_REQUEST["amount"]."');";
 * * returns:
 * ************************************************************************* */
//  function authorize_process($order_id, $order_total, &$d) {
//$DEBUG=y;

if (isset($DEBUG) && $DEBUG)
    echo "authorize_process<BR>\n";

//your location of cURL abd authorize.net info
$curl = $global_vars["ANET_CURL_PATH"];
$my_authnet_userid = $global_vars["ANET_USER_NAME"];
$my_authnet_passwd = $global_vars["ANET_USER_PW"];

if (isset($DEBUG) && $DEBUG)
    echo "$curl $my_authnet_userid<BR>\n";
//fill in billing information

$cc_exp = (isset($_REQUEST["cardExpiration"]) && $_REQUEST["cardExpiration"])?$_REQUEST["cardExpiration"]:NULL;
$total = (isset($_REQUEST["amount"]) && $_REQUEST["amount"])?$_REQUEST["amount"]:NULL;
$total = str_replace("$", " ", $total);  // make certain $ is not if field
$cc_number = (isset($_REQUEST["cardNumber"]) && $_REQUEST["cardNumber"])?$_REQUEST["cardNumber"]:NULL;
$first_name = (isset($_REQUEST["cardName"]) && $_REQUEST["cardName"])?$_REQUEST["cardName"]:NULL;
$last_name = (isset($_REQUEST["cardName"]) && $_REQUEST["cardName"])?$_REQUEST["cardName"]:NULL;
$invoiceno = (isset($_REQUEST["invoiceNumber"]) && $_REQUEST["invoiceNumber"])?$_REQUEST["invoiceNumber"]:NULL;
$description =  (isset($_REQUEST["website"]) && $_REQUEST["website"])?$_REQUEST["website"]:NULL . " "; 
$description .= (isset($_REQUEST["category"]) && $_REQUEST["category"])?$_REQUEST["category"]:NULL . " ";
$description .= (isset($_REQUEST["description"]) && $_REQUEST["description"])?$_REQUEST["description"]:NULL;
//not used -- use if we turn on AVS
//$billing_address = $dbbt->f("address_1");
//$billing_city = $dbbt->f("city");
//$billing_state = $dbbt->f("state");
//$billing_zip = $dbbt->f("zip");
//var_dump($_REQUEST);
//must be port 443,  epiphany's default
//  $cmd = "$curl -d  '"
$data = ""
//required
        . "x_version=3.1"
        . "&x_delim_Data=TRUE"
        . "&x_login=$my_authnet_userid"
        // ."&x_login=zwyp72hu"
        . "&x_password=$my_authnet_passwd"
        //."&x_tran_key =Ha8xXZcpVSxLugU2"
        //."&x_test_request=TRUE"
        . "&x_amount=$total"
        . "&x_card_num=$cc_number"
        . "&x_exp_date=$cc_exp"
        . "&x_type=AUTH_CAPTURE"
//optional
        . "&x_First_Name=$first_name"
        . "&x_last_name=$last_name"
        . "&x_invoice_num=$invoiceno"
        . "&x_description=" . urlencode($description)
        . "&x_Email=" . urlencode((isset($_REQUEST["email"]) && $_REQUEST["email"])?$_REQUEST["email"]:"");
//  -- Now capture funds on this x_Auth_Code. ??
//VOID -- any of the above transactions
//Currency codes: USD, EUR
//       ."&x_ADC_URL=FALSE"
//       ."&x_Address=" . urlencode($billing_address)
//       ."&x_City=" . urlencode($billing_city)
//       ."&x_Phone=" . urlencode($dbbt->f("phone_1"))
//       ."&x_State=$billing_state"
//       ."&x_Zip=$billing_zip"
//         ."' --cacert /usr/share/ssl/certs/ca-bundle.crt https://secure.authorize.net:443/gateway/transact.dll";

if (isset($DEBUG) && $DEBUG)
    echo "$cmd<BR>\n";

// do it
//    exec($cmd, $authorize, $ret);
$authNetURL = "https://secure.authorize.net/gateway/transact.dll";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $authNetURL);
curl_setopt($ch, CURLOPT_USERAGENT, ANET_DESCRIPTION);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//By default, the response fields will be delimited with a comma.
//the response fields.
//By default, the response fields will be delimited with a comma.
//the response fields.
// execute curl and return values
$return_string = curl_exec($ch);

// close connection
curl_close($ch);

//$auth_return = split("\|", $authorize[0]);
$auth_return = split(",", $return_string);

//The response fields will not be encapsulated by default.
//The merchant can configure the encapsulation character.
//It is recommended that the merchant override the system default and set an
//encapsulation character -- no encapsulation is currently set for epiphany
if (isset($DEBUG) && $DEBUG)
    while (list($key, $val) = each($auth_return)) {
        echo "key=$key val=$val<BR>\n";
    }

$error = "";
if ($auth_return[0] == 1) {
    //message + confirmation code
    $authMessage = $auth_return[3] . " " . $auth_return[4];
//      $approval_code = $auth_return[5];
//DEBUG echo "<B>approval_code=$approval_code</B><BR>\n";
//      $auth_code = $auth_return[4];
//      $avs_code = $auth_return[5];
//      $trans_id = $auth_return[6];
//      while(list($key,$val)=each($auth_return)) {
//        if ($val != "" ) .= $key."=".$val."<br>";
//      }
} elseif ($auth_return[0] == 2) {
    $authMessage = "This charge was not processed because: $auth_return[3]<br>";
} elseif ($auth_return[0] == 3) {
    $authMessage = "This charge was not processed because: $auth_return[3]<br>";
}

//  }  // end of autorize_process
?>
